﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.SiteAdmin;
using Jzy.Core.Common;
using Jzy.Core.Common.Web;
using Jzy.Core.Utility;
using Jzy.Model.SiteAdmin;

namespace Jzy.Web.SiteAdmin.Job
{
    public partial class List : PageList
    {
        private JobBO _job = new JobBO();
        private string strOrders;

        private JobBO Job
        {
            get
            {
                if (_job == null)
                {
                    _job = new JobBO();
                }
                return _job;
            }
        }


        private PagerSortSetting _setting;

        protected void Page_Load(object sender, EventArgs e)
        {
            _setting = this.Footer1.InitPager(new BindViewHandler(BindView));

            if (!IsPostBack)
            {
                
                
                BindView();
               
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
            strOrders = hidOrders.Value;
            hidOrders.Value = "";

            JSUtil.SetTableHeight("list", 145);

        }


        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "职位");

            string url = "Edit.aspx";

            JSUtil.ShowForm(lnkAdd, "添加职位", null, url, 470, 380);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);

        }

        private void BindView()
        {

            LVUtil.Init(lv, lvHeader);

            IEnumerable<JobInfo> list = this.Job.GetList();
            _setting.RecordCount = list.Count();

            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + _setting.RecordCount + "</b>&nbsp;个职位&nbsp;]";

            lv.DataSource = list.Skip(_setting.SkipCount).Take(_setting.PageSize);
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();


        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);

            //要删除相关的图片和附件
            this.Job.Delete(lstSelectIDs);
            BindView();
        }

        protected void btnSaveSequence_Click(object sender, EventArgs e)
        {
            List<int> ids = new List<int>();
            List<int> orders = new List<int>();
            StrUtil.SplitIDOrders(ids, orders, strOrders);
            this.Job.SaveOrders(ids, orders);
            BindView();
        }


        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

  

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");

                JSUtil.ShowForm(lnkEdit, "修改职位", null, "Edit.aspx?ID=" + strID, 470, 380);
                JSUtil.ShowForm(lnkName, "修改职位", null, "Edit.aspx?ID=" + strID, 470, 380);

            }
        }
    }
}
